'''
Created on 31/03/2010

@author: miguel
'''
import pygame
from pygame.sprite import Sprite
from vec2d import vec2d

class PlayerPanel(object):
    (LEFT, RIGHT) = range(2)

    PLAYER_HOUSE_IMAGE_3_SUPPORTS = './data/player_house_3.png'
    PLAYER_HOUSE_IMAGE_2_SUPPORTS = './data/player_house_2.png'
    PLAYER_HOUSE_IMAGE_1_SUPPORTS = './data/player_house_1.png'
    PLAYER_HOUSE_IMAGE_0_SUPPORTS = './data/player_house_0.png'
    SUPPORT1_IMAGE_ON = './data/led_support.png'
    SUPPORT1_IMAGE_OFF = './data/led_support_soft.png'
    SURPRISE_IMAGE_ON = './data/led_surprise.png'
    SURPRISE_IMAGE_OFF = './data/led_surprise_soft.png'

    def __init__(self, screen, game):
        #Sprite.__init__(self)

        self.screen = screen
        self.game = game

        self.support1 = False
        self.support2 = False
        self.support3 = False
        self.surprise1 = False
        self.surprise2 = False
        self.supports_discovered = 0
        self.surprises_discovered = 0
        self.movement_side = self.LEFT
        self.counter = 0

        self.position_house = vec2d(650, 480)
        self.image_player_house_3 = pygame.image.load(self.PLAYER_HOUSE_IMAGE_3_SUPPORTS)
        self.image_player_house_2 = pygame.image.load(self.PLAYER_HOUSE_IMAGE_2_SUPPORTS)
        self.image_player_house_1 = pygame.image.load(self.PLAYER_HOUSE_IMAGE_1_SUPPORTS)
        self.image_player_house_0 = pygame.image.load(self.PLAYER_HOUSE_IMAGE_0_SUPPORTS)
        self.image_player_house = self.image_player_house_3
        self.rect_player_house = self.image_player_house.get_rect().move(self.position_house)

        self.position_support1 = vec2d(570, 480)
        self.image_led_support_off = pygame.image.load(self.SUPPORT1_IMAGE_OFF)
        self.image_led_support_on = pygame.image.load(self.SUPPORT1_IMAGE_ON)
        self.image_led_support1 = self.image_led_support_off
        self.rect_led_support1 = self.image_led_support1.get_rect().move(self.position_support1)

        self.position_support2 = vec2d(570, 504)
        self.image_led_support2 = self.image_led_support_off
        self.rect_led_support2 = self.image_led_support2.get_rect().move(self.position_support2)

        self.position_support3 = vec2d(570, 528)
        self.image_led_support3 = self.image_led_support_off
        self.rect_led_support3 = self.image_led_support3.get_rect().move(self.position_support3)

        self.position_surprise1 = vec2d(570, 552)
        self.image_led_surprise_off = pygame.image.load(self.SURPRISE_IMAGE_OFF)
        self.image_led_surprise_on = pygame.image.load(self.SURPRISE_IMAGE_ON)
        self.image_led_surprise1 = self.image_led_surprise_off
        self.rect_led_surprise1 = self.image_led_surprise1.get_rect().move(self.position_surprise1)

        self.position_surprise2 = vec2d(570, 576)
        self.image_led_surprise2 = self.image_led_surprise_off
        self.rect_led_surprise2 = self.image_led_surprise2.get_rect().move(self.position_surprise2)

    def set_support_discovered(self):
        if self.supports_discovered <= 3:
            self.supports_discovered = self.supports_discovered + 1
            if self.supports_discovered == 1:
                self.image_led_support1 = self.image_led_support_on
                self.image_player_house = self.image_player_house_2
            elif self.supports_discovered == 2:
                self.image_led_support2 = self.image_led_support_on
                self.image_player_house = self.image_player_house_1
            elif self.supports_discovered == 3:
                self.image_led_support3 = self.image_led_support_on
                self.image_player_house = self.image_player_house_0

    def set_surprise_discovered(self):
        if self.surprises_discovered <= 2:
            self.surprises_discovered = self.surprises_discovered + 1
            if self.surprises_discovered == 1:
                self.image_led_surprise1 = self.image_led_surprise_on
            if self.surprises_discovered == 2:
                self.image_led_surprise2 = self.image_led_surprise_on

    def update(self):
        if self.supports_discovered == 1:
            if self.counter > 3:
                if self.movement_side == self.LEFT:
                    self.position_house = vec2d(649, 480)
                    self.rect_player_house = self.image_player_house.get_rect().move(self.position_house)
                    self.movement_side = self.RIGHT
                elif self.movement_side == self.RIGHT:
                    self.position_house = vec2d(651, 480)
                    self.rect_player_house = self.image_player_house.get_rect().move(self.position_house)
                    self.movement_side = self.LEFT
                self.counter = 0
        if self.supports_discovered == 2:
            if self.counter > 1:
                if self.movement_side == self.LEFT:
                    self.position_house = vec2d(649, 480)
                    self.rect_player_house = self.image_player_house.get_rect().move(self.position_house)
                    self.movement_side = self.RIGHT
                elif self.movement_side == self.RIGHT:
                    self.position_house = vec2d(651, 480)
                    self.rect_player_house = self.image_player_house.get_rect().move(self.position_house)
                    self.movement_side = self.LEFT
                self.counter = 0
        if self.supports_discovered == 3:
            self.game.winner = self.game.COMPUTER
            pass
        self.counter = self.counter + 1

    def draw(self):
        pygame.draw.rect(self.screen, (200, 200, 255), (600, 480, 200, 120))
        self.screen.blit(self.image_player_house, self.rect_player_house)
        self.screen.blit(self.image_led_support1, self.rect_led_support1)
        self.screen.blit(self.image_led_support2, self.rect_led_support2)
        self.screen.blit(self.image_led_support3, self.rect_led_support3)
        self.screen.blit(self.image_led_surprise1, self.rect_led_surprise1)
        self.screen.blit(self.image_led_surprise2, self.rect_led_surprise2)
